from django.db import models

# Create your models here.
# 创建表的关系 先将基表创建出来，然后再添加外键字段


class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=8, decimal_places=2)

    publish = models.ForeignKey(to='Publish', to_field='id', on_delete=models.CASCADE)  # 默认就是与主键做关联

    authors = models.ManyToManyField(to='Author')
    # 自动在字段后面添加_id,models.ManyToManyField会创建第三张表
    # ForeignKey和OneToOne要添加级联属性,on_delete=models.CASCADE


class Publish(models.Model):
    name = models.CharField(max_length=32)
    addr = models.CharField(max_length=32)


class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    author_detail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)


class AuthorDetail(models.Model):
    phone = models.BigIntegerField()
    addr = models.CharField(max_length=32)